package com.csw.compose.ui.module.state

import androidx.compose.animation.animateColor
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import com.csw.compose.theme.pagePadding
import com.csw.compose.ui.Destination
import com.csw.compose.ui.components.BodyText
import com.csw.compose.ui.components.LabelText
import com.csw.compose.ui.components.NormalSpacer
import com.csw.compose.ui.components.OlItem
import com.csw.compose.ui.components.PrimaryTitle
import com.csw.compose.ui.components.UlItem
import com.csw.compose.ui.module.BasePage

@Preview
@Composable
fun RememberPagePreview() {
    BasePage({}, "不受重组影响的变量（remember）") {
        RememberPage()
    }
}

class RememberPageData {
    companion object : Destination("RememberPage")
}

@Composable
fun RememberPage() {
    Column(
        modifier = Modifier
            .padding(horizontal = pagePadding)
            .verticalScroll(rememberScrollState())
    ) {
        PrimaryTitle("简介")
        BodyText("组合函数比较特殊，在状态变化时，会导致订阅该状态的组件重组，由于重组时函数重新执行会导致该函数的局部变量重新创建导致丢失状态")
        NormalSpacer()

        val backgroundState =
            rememberInfiniteTransition("backgroundInfiniteTransition").animateColor(
                Color.Green,
                Color.Blue,
                infiniteRepeatable(tween(), RepeatMode.Reverse)
            )
        ClickTimes(backgroundState.value)
        ClickTimes2(backgroundState.value)
        UlItem("remember")
        LabelText("通过该方法创建返回的变量会被存储，在首次创建之后，即使组件重组，也不会重新创建，也就是说它的作用域是可以跟随该组件不受重组影响的")

        NormalSpacer()
        OlItem(1, "key")
        LabelText("用于控制是否重建变量，若重组时，key更新了，则会重新创建")

        NormalSpacer()
        val list = remember { listOf("1", "2") }
        val (value, setValue) = remember { mutableIntStateOf(0) }
        OlItem(2, "返回值")
        LabelText("一般来说，返回值就是remember函数返回的泛型类型")
        LabelText("但若是State类型，可以直接表示为value与getValue方法（高阶函数，方法也是对象）")

        NormalSpacer()
        var intValue by remember { mutableIntStateOf(0) }
        OlItem(3, "委托")
        LabelText("当存储State类型时，通过by关键字，将变量的setter、getter方法委托给State对象，这样可以将变量作为真实类型使用")
    }
}

@Composable
fun ClickTimes(background: Color) {
    var times = 0
    Button({ times++ }, modifier = Modifier.background(background)) {
        Text("点击：${times}")
    }
}

@Composable
fun ClickTimes2(background: Color) {
    val times = remember { mutableIntStateOf(0) }
    Button({ times.intValue++ }, modifier = Modifier.background(background)) {
        Text("点击：${times.intValue}")
    }
}
